package com.kunpan.admin.common.utils;

import com.kunpan.admin.common.redis.RedisCache;
import com.kunpan.admin.constant.Constants;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

public class CaptchaUtils {

    private final static int width = 120;
    private final static int height = 40;
    private final static int length = 4;

    public static Map<String, Object> generateCaptchaImage(RedisCache redisCache) throws IOException {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();

        // 设置背景颜色（渐变背景）
        Graphics2D g2d = (Graphics2D) g;
        Color startColor = new Color(255, 255, 255);
        Color endColor = new Color(200, 200, 255);
        GradientPaint gradient = new GradientPaint(0, 0, startColor, width, height, endColor);
        g2d.setPaint(gradient);
        g2d.fillRect(0, 0, width, height);

        // 生成随机字符
        String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        StringBuilder captchaText = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            captchaText.append(characters.charAt(index));
        }
        Map<String, Object> map = new HashMap<>();
        String uuid = UUID.randomUUID().toString();
        //将正确的验证码存入缓存，并设置存活1分钟
        redisCache.setCacheObject(Constants.CAPTCHA_KEY + uuid, captchaText.toString());
        redisCache.expire(Constants.CAPTCHA_KEY + uuid,60);
        map.put("uuid", uuid);

        // 渲染字符到图片（带旋转和不同颜色）
        g.setFont(new Font("Verdana", Font.BOLD, 28));
        for (int i = 0; i < captchaText.length(); i++) {
            g.setColor(getRandomColor());
            // 随机旋转字符
            double rotation = Math.toRadians(random.nextInt(30) - 15);  // 旋转角度
            g2d.rotate(rotation, (i + 1) * 20, height / 2); // 旋转点
            g.drawString(String.valueOf(captchaText.charAt(i)), (i + 1) * 20, 25);
            g2d.rotate(-rotation, (i + 1) * 20, height / 2); // 旋转恢复
        }

        // 添加噪点
        g.setColor(new Color(100, 100, 100));
        for (int i = 0; i < 150; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            g.fillRect(x, y, 2, 2); // 增加噪点大小
        }

        // 添加干扰线
        g.setColor(new Color(150, 150, 150));
        for (int i = 0; i < 3; i++) {
            int x1 = random.nextInt(width);
            int y1 = random.nextInt(height);
            int x2 = random.nextInt(width);
            int y2 = random.nextInt(height);
            g.drawLine(x1, y1, x2, y2);
        }

        // 添加边框
        g.setColor(new Color(0, 0, 0));
        g.drawRect(0, 0, width - 1, height - 1);

        // 保存图片到字节数组
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", baos);
        baos.flush();
        byte[] imageBytes = baos.toByteArray();
        baos.close();
        g.dispose();
        map.put("imageBytes",imageBytes);

        return map;
    }

    // 随机生成颜色
    private static Color getRandomColor() {
        Random random = new Random();
        int r = random.nextInt(256);
        int g = random.nextInt(256);
        int b = random.nextInt(256);
        return new Color(r, g, b);
    }
}
